<%@page import="com.buss.core.dic.RecordStatus"%>
<%@page import="com.buss.product.utils.ProductStaticParam"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Wcms内容管理系统</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		$(document).ready(function(){						
			setGridData();
			loadTree();
			
			resize();
			$(window).resize(resize);
			$("#search-btn").button({iconCls:'icon-search',handler:function(){
    			var jsonParam = syscore.formToJson($("#_searchForm"));
				$("#tabledata").jqGrid("setGridParam",{page:1,postData:jsonParam}).trigger("reloadGrid");
			}});
			$("#search-clear").button({iconCls:'icon-cancel',handler:function(){
				$("#_searchForm").resetForm();
    			$("#search-btn").trigger("click");
			}});
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var node = $("#customerClassTree").treeview("getSelectedNode");
				if(!node || node.id == '0'){
					$.alert('提示','请选择要增加往来单位的分类信息!',300,140,'info');
					return;
				}
				editPage("addpage",node.id);				
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				editPage("editpage",selRows[0]);				
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/customer/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除往来单位信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});			
			}});
			$("select[name='salesId']").combo({
				filter:true,
				onChange:function(newValue,oldValue){
					//alert(newValue);
				}
			});
			
			$("#customer_contacts").button({iconCls:'icon-add',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','请选择往来单位信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var panel = "<div>"+
					"<div class='contactsContext' style='padding:5px;'>" +
					"<table id='contactsContext_data'/>"+
					"</div></div>";
				var dlg = $(panel).dialog({
					height:450,
					width:700,
					title:'单位联系人信息',
					iconCls:'icon',
					topTools:[
					          {text:'新增',type:'button',iconCls:'icon-add',plain:true,handler:function(){
					        	  editContactsPage("addpage",selRows[0],dlg);
					          }},
							  {text:'修改',type:'button',iconCls:'icon-edit',plain:true,handler:function(){
								    var cSelRows = dlg.find("#contactsContext_data").jqGrid('getGridParam',"selarrrow");
									if(cSelRows.length == 0){
										$.alert('提示','请选择要修改的联系人信息!',300,140,'info');
										return;
									}
									if(cSelRows.length > 1){
										$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
										return;
									}								  
								    editContactsPage("editpage",cSelRows[0],dlg);
					          }},
							  {text:'删除',type:'button',iconCls:'icon-remove',plain:true,handler:function(){
								  var selRows = dlg.find("#contactsContext_data").jqGrid('getGridParam',"selarrrow");
									if(selRows == 0){
										$.alert('提示','你没有选择要删除的数据!',300,140,'info');
										return;
									}
									$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
										var str = "";
										for(var i = 0;i < selRows.length;i++){
											if(i < selRows.length - 1){
												str += "ids=" + selRows[i] + "&";
											}else{
												str += "ids=" + selRows[i];
											}
										}
										$.post("<%=basePath%>admin/customercontacts/delete.do",str,function(rsData){
											if(rsData.message.code != '0'){
												$.alert('错误','删除单位联系人信息失败!原因:' + rsData.message.mess,300,140,'error');
												return;
											}
											dlg.find("#contactsContext_data").jqGrid({page:1}).trigger('reloadGrid');
										},"json");
									});		
					          }},
					          {text:'关闭',type:'button',iconCls:'icon-cancel',plain:true,handler:function(){
					        	  dlg.dialog("destroy");
					          }},
					          ]
				});
				
				var url = "<%=basePath%>admin/customercontacts/list.do?customerId=" + selRows[0];
				var colNames=['联系人','移动电话','家庭电话','单位电话','单位传真'];
				var colModel=[
								{name:'name',index:'name', width:120, sortable:false,align:'left',formatter:function(newCell,opt,rowObj){
									return newCell + "("+(rowObj.salutation=='man'?'男士':'女士')+")";
								}},
								{name:'mobile',index:'mobile', width:80,align:'left',sortable:false},
								{name:'familyPhone',index:'familyPhone', width:80,align:'left',sortable:false},
								{name:'companyPhone',index:'companyPhone', width:80,align:'left',sortable:false},
								{name:'companyFax',index:'companyFax', width:80,align:'left',sortable:false},
				              ];				
				dlg.find("#contactsContext_data").jqGrid({
					height:355,
					width:680,
					url:url,
					datatype:'json',
					multiselect:true,
					multiboxonly:true,
					colNames:colNames,
					colModel:colModel,
					jsonReader:{
						id:'id',
						root:'result.data',
						page:'result.page.curPageNo',
						total:'result.page.totalPage',
						repeatitems:false
					},
					prmNames:{
						page:'page.curPageNo',
						rows:'page.pageSize',
						sort:'orderField',
						order:'orderType'		
					}
				});
			}});
			$("#customer_accounts").button({iconCls:'icon-add',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','请选择往来单位信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var panel = "<div>"+
					"<div class='accountContext' style='padding:5px;'>" +
					"<table id='accountContext_data'/>"+
					"</div></div>";
				var dlg = $(panel).dialog({
					height:450,
					width:700,
					title:'单位账号信息',
					iconCls:'icon',
					topTools:[
					          {text:'新增',type:'button',iconCls:'icon-add',plain:true,handler:function(){
					        	  editAccountPage("addpage",selRows[0],dlg);
					          }},
							  {text:'修改',type:'button',iconCls:'icon-edit',plain:true,handler:function(){
								    var cSelRows = dlg.find("#accountContext_data").jqGrid('getGridParam',"selarrrow");
									if(cSelRows.length == 0){
										$.alert('提示','请选择要修改的账号信息!',300,140,'info');
										return;
									}
									if(cSelRows.length > 1){
										$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
										return;
									}								  
								    editAccountPage("editpage",cSelRows[0],dlg);
					          }},
							  {text:'删除',type:'button',iconCls:'icon-remove',plain:true,handler:function(){
								  var selRows = dlg.find("#accountContext_data").jqGrid('getGridParam',"selarrrow");
									if(selRows == 0){
										$.alert('提示','你没有选择要删除的数据!',300,140,'info');
										return;
									}
									$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
										var str = "";
										for(var i = 0;i < selRows.length;i++){
											if(i < selRows.length - 1){
												str += "ids=" + selRows[i] + "&";
											}else{
												str += "ids=" + selRows[i];
											}
										}
										$.post("<%=basePath%>admin/customeraccount/delete.do",str,function(rsData){
											if(rsData.message.code != '0'){
												$.alert('错误','删除单位账号信息失败!原因:' + rsData.message.mess,300,140,'error');
												return;
											}
											dlg.find("#accountContext_data").jqGrid({page:1}).trigger('reloadGrid');
										},"json");
									});		
					          }},
					          {text:'关闭',type:'button',iconCls:'icon-cancel',plain:true,handler:function(){
					        	  dlg.dialog("destroy");
					          }},
					          ]
				});
				
				var url = "<%=basePath%>admin/customeraccount/list.do?customerId=" + selRows[0];
				var colNames=['账户名称','账户号码','是否默认账户',"账户说明"];
				var colModel=[
								{name:'accountName',index:'accountName', width:120, sortable:false,align:'left'},
								{name:'accountNumber',index:'accountNumber', width:100,align:'left',sortable:false},
								{name:'isDefault',index:'isDefault', width:80,align:'center',sortable:false,formatter:function(cellVal){
									if(cellVal){
										return "是";
									}else{
										return "否";
									}
								}},
								{name:'intro',index:'intro', width:200,align:'left',sortable:false}
				              ];				
				dlg.find("#accountContext_data").jqGrid({
					height:355,
					width:680,
					url:url,
					datatype:'json',
					multiselect:true,
					multiboxonly:true,
					colNames:colNames,
					colModel:colModel,
					jsonReader:{
						id:'id',
						root:'result.data',
						page:'result.page.curPageNo',
						total:'result.page.totalPage',
						repeatitems:false
					},
					prmNames:{
						page:'page.curPageNo',
						rows:'page.pageSize',
						sort:'orderField',
						order:'orderType'		
					}
				});
			}});
		});
		
		/**新增、修改单位账号信息*/
		function editAccountPage(type,id,dialog){
			var url = "<%=basePath%>admin/customeraccount/";
			var saveUrl = url;
			var title = "新增账户信息";
			if(type == "editpage"){
				title = "修改账户信息";
				url += "editpage.do?id=" + id;
				saveUrl += "edit.do";
			}else{
				url += "addpage.do?id=" + id;
				saveUrl += "add.do";
			}
			var editDlg = syscore.editDialog(title,url,'',550,350,saveUrl,function(rsData){
				if(rsData.message.code != '0'){
					$.alert('错误','保存数据失败，原因：' + rsData.message.mess,300,140,'error');
    				return;
				}
				dialog.find("#accountContext_data").jqGrid().trigger('reloadGrid');
    			editDlg.dialog("destroy");
			},function(){
				
			});
		}
		
		/**新增、修改单位联系人信息*/
		function editContactsPage(type,id,dialog){
			var url = "<%=basePath%>admin/customercontacts/";
			var saveUrl = url;
			var title = "新增联系人信息";
			if(type == "editpage"){
				title = "修改联系人信息";
				url += "editpage.do?id=" + id;
				saveUrl += "edit.do";
			}else{
				url += "addpage.do?id=" + id;
				saveUrl += "add.do";
			}
			var editDlg = syscore.editDialog(title,url,'',600,420,saveUrl,function(rsData){
				if(rsData.message.code != '0'){
					$.alert('错误','保存数据失败，原因：' + rsData.message.mess,300,140,'error');
    				return;
				}
				dialog.find("#contactsContext_data").jqGrid().trigger('reloadGrid');
    			editDlg.dialog("destroy");
			},function(){
				
			});
		}
		
		/**新增、修改页面*/
		function editPage(type,id){    		
    		var url = "<%=basePath%>admin/customer/";
    		var pageUrl = "";
    		var saveUrl = "";
    		var title = "新增往来单位信息";
    		if(type == "editpage"){
    			title = "修改往来单位信息";
    			saveUrl = url + "edit.do";
    			pageUrl = url + "editpage.do?id=" + id;
    		}else{
    			saveUrl = url + "add.do";
    			pageUrl = url + "addpage.do?id=" + id;
    		}
    		var editDlg = syscore.editDialog(title,pageUrl,'',700,400,saveUrl,function(rsData){
    			if(rsData.message.code != '0'){
    				$.alert('错误','保存数据失败，原因：' + rsData.message.mess,300,140,'error');
    				return;
    			}
    			$("#tabledata").jqGrid().trigger('reloadGrid');
    			editDlg.dialog("destroy");
    		},function(){
    			editDlg.find("select[name='source.id']").combo({editable:false});
    		});
    	}
		
		
		
		function setGridData(){
			var url = "<%=basePath%>admin/customer/list.do?classId=0";
			var colNames = ['单位名称','所属类别', '来源','电话','地址','邮政编码','当前状态'];
			var colModel = [
			                {name:'name',index:'name', width:150,align:'left'},
						    {name:'customerClass.name',index:'className', width:80,align:'left',sortable:false},							 
							{name:'source.name',index:'sourceName', width:80,align:'left',sortable:false}, 
							{name:'telPhone',index:'telPhone',width:80,align:'left',sortable:true},
							{name:'address',index:'address',width:120,align:'left'},
							{name:'post',index:'post',width:80,align:'left'},
							{name:'status',index:'status',width:80,align:'center',formatter:syscore.formaterStatus}
						];
			syscore.setGridData("#tabledata","tablepage",url,colNames,colModel,true);
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				name:'往来单位分类信息',			
				isParent:true,
				subClasses:true,
				open:true
			}];
			$("#customerClassTree").treeview({
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'subClasses',
				nameCol:'name',
				expandSpeed:"",
				async:true,
				asyncUrl:function(treeNode){
					return "<%=basePath%>admin/customerclass/list.do?parentId=" + treeNode.id;	
				},
				root:{isRoot:true,nodes:root},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						var url = $("#tabledata").jqGrid("getGridParam","url");
						$("#tabledata").jqGrid("setGridParam",{"url":"<%=basePath%>admin/customer/list.do?classId="+treeNode.id}).trigger('reloadGrid');
					}
				}
			});
		}

		
		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#customerClassTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="customerClassTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	     <div class="nav_search" style="height:56px;">
	     	<form id='_searchForm' action="" name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle" align="right">客户名称：</td>
		    			<td valign="middle" align="left">
		    				<input type="text" size="20" maxlength="100" class="text" name="name"/>
		    			</td>
		    			<td valign="middle" style="padding-left:5px;" align="right">拼音码：</td>
		    			<td valign="middle" align="left">
		    				<input type="text" size="20" maxlength="100" class="text" name="mnemonic"/>
		    			</td>
		    			<%--<td valign="middle" style="padding-left:5px;" align="right">业务员：</td>
		    			<td valign="middle" align="left">
		    				<select name="salesId" style="width:100px;display:none;">
		    					<option value="">--全部--</option>
		    					<c:forEach items="${users}" var="user">
		    						<option value="${user.id }">${user.userName }</option>
		    					</c:forEach>
		    				</select> 
		    			</td>--%>
		    		</tr>
		    		<tr>
		    			<td valign="middle" align="right">单位类型：</td>
		    			<td valign="middle" align="left">
		    				<input type="checkbox" id="isCustomer" name="isCustomer" value="true" checked="checked"/><label for="isCustomer">客户</label>
		    				<input type="checkbox" id="isSupplier" name="isSupplier" value="true" checked="checked"/><label for="isSupplier">供应商</label>
		    			</td>
		    			<td valign="middle" align="right">当前状态：</td>
		    			<td valign="middle"  align="left" style="padding-left:2px;">
		    				<input type="radio" id="all" name="status" checked="checked" value=""/><label for="all">所有</label>
		    				<%
		    					for(RecordStatus status : RecordStatus.values()){
		    				%>
		    					<input type="radio" id="<%=status %>" name="status" value="<%=status%>"/><label for="<%=status%>"><%=status.getValue() %></label>
		    				<%
		    					}
		    				%>
		    			</td>
		    			
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    			<td style="padding-left:5px;">
			    			<a id="search-clear" href="javascript:void(0);">清 空</a>
			    		</td>
		    		</tr>
		    	</table>
	    	</form>
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:15px;">
	    				<a id="customer_contacts" href="javascript:void(0);">管理联系人</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="customer_accounts" href="javascript:void(0);">账户信息管理</a>
	    			</td>
	    			
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div>     
  </body>
</html>
